package com.example.myretrofit2.unity;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class RequestInterceptor implements Interceptor {
    Context context;

    public RequestInterceptor(Context context) {
        this.context = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        //Request request = chain.request();
        //Response response = chain.proceed(request);
        Request.Builder builder =chain.request().newBuilder();
        SharedPreferences preferences = context.getSharedPreferences("Cookie", Context.MODE_PRIVATE);
        String JSESSIONID = preferences.getString("JSESSIONID"," ");
//        System.out.println("0"+JSESSIONID);
        if(!TextUtils.isEmpty(JSESSIONID)){
            builder.addHeader("Cookie",JSESSIONID);
        }
        return chain.proceed(builder.build());
    }
}
